package gbench.mydata.driver.core.jdbc;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import gbench.common.tree.LittleTree.IRecord;

/**
 * 第一步：导入实现java.sql这个包中接口的的各个厂商实现的驱动包 <br>
 * 第二步：加载并注册驱动程序（java.sql.Driver接口的实现类）<br>
 * 第三步：获取java.sql.Connection的实现类对象（数据库连接对象） <br>
 * 第四步：编写Statement/PreparedStatement <br>
 * 第五步：执行sql语句 <br>
 * 第六步：如果是查询语句，那么会得到ResultSet对象，对ResultSet对象进行一系列处理，然后关闭ResultSet对象；如果是增删改操作，那么省略这一步
 * <br>
 * 第七步：关闭Statement对象 <br>
 * 第八步：关闭Connection对象 <br>
 * 
 * @author gbench
 *
 */
public abstract class AbstractDriver implements Driver {

    @Override
    public abstract Connection connect(String url, Properties info) throws SQLException;

    /**
     * "jdbc:gbench:mydata://localhost/mydb3?nossl"
     */
    @Override
    public boolean acceptsURL(String url) throws SQLException {
        if (url == null) {
            return false;
        }

        final var matcher = Pattern
                .compile("\\s*jdbc\\s*:\\s*mydata\\s*:\\s*([\\w]+)\\s*:\\s*//([^/]+)/([\\w\\d]+)(.*)?",
                        Pattern.CASE_INSENSITIVE)
                .matcher(url);

        // println(url);
        if (matcher.matches()) {
            final var type = matcher.group(1);// type
            final var host = matcher.group(2);// host
            final var sheet = matcher.group(3);// sheet
            final var file = matcher.group(4);// file
            driverInfo = IRecord.REC("type", type, "host", host, "sheet", sheet, "file", file);
            // println(driverInfo);
            return true;
        }

        return false;
    }

    @Override
    public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {

        return null;
    }

    @Override
    public int getMajorVersion() {

        return 0;
    }

    @Override
    public int getMinorVersion() {

        return 0;
    }

    @Override
    public boolean jdbcCompliant() {

        return false;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {

        return null;
    }

    protected IRecord driverInfo;

}
